/**启用OpenFeign：核心依赖库是spring-cloud-starter-openfeign
 * 1）在 @CommpomentScan 所扫描不到的位置@Configuration定义配置文件，如负载均衡策略
 * 2）启动类上添加@EnableFeignClients，
 * 3）定义接口，并用@FeignClient(value = "服务名")注解，使用上述配置文件
 * 4）注入并使用上述接口实例调用接口方法
 */
package com.example.demolbfeignclient;


import config.ApplicationConf;
import feign.RequestLine;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.loadbalancer.reactive.ReactiveLoadBalancer;
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClient;
import org.springframework.cloud.loadbalancer.core.RandomLoadBalancer;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;


@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@LoadBalancerClient(name = "userlogin", configuration = ApplicationConf.class)
public class DemoLBFeignClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoLBFeignClientApplication.class, args);
    }

    @FeignClient(name="userlogin",configuration = ApplicationConf.class)
    public interface UserloginService{
//      @RequestMapping(value = "/login",method = RequestMethod.GET)
        @RequestLine("GET /login")
        Map myUserlogin();
    }

    @Bean
    public ReactiveLoadBalancer<ServiceInstance> reactorServiceInstanceLoadBalancer(Environment environment,
          LoadBalancerClientFactory loadBalancerClientFactory) {
          String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
        return new RandomLoadBalancer(
                loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class), name);
    }
}
